<?php
class ShopAction extends Action{
	
    function _initialize() {
		
	}
	public function index() { 
	  $id = empty($_GET['tid'])? '' : intval($_GET['tid']);
	  
	  $map  =  1;
	  if($id){
	   $map = 'gtype='.$id;
	  }elseif($_POST['k']){
	   $search_key   =  $this->__getSearchKey ();
	   $map = "title LIKE '%{$search_key}%'";
	  }
	  
	    $typelist = M('goods_type')->order('display_id ASC')->findAll();
		//print_r($typelist);
	    $goods = M ('');
		$db_prefix  =  C('DB_PREFIX');
	    $count = M('goods')->where($map)->count();    //计算总数
		$p = new Page ( $count, 15 );
		$goodslist= $goods->field('g.id, g.uid, g.uname, g.title, g.gprice, g.gnumber, g.location, g.school, g.gimage, g.utime, bf.qq')
		->table("{$db_prefix}goods AS g LEFT JOIN {$db_prefix}user_basicfile AS bf ON g.uid=bf.uid")
		->where($map)->limit($p->firstRow.','.$p->listRows)->order('utime DESC')->findAll ();
	    $page = $p->show ();
		$this->assign ( "page", $page );
		//echo $goods->getLastSql();
		$this->assign('typelist',$typelist);
		$this->assign('goodslist',$goodslist);
		$this->setTitle('销售大厅');
		$this->display();
	}
	
	
	
    private function __getSearchKey() {
		$key = '';
		// 为使搜索条件在分页时也有效，将搜索条件记录到SESSION中
		if (isset ( $_REQUEST ['k'] ) && ! empty ( $_REQUEST ['k'] )) {
			if ($_GET ['k']) {
				$key = html_entity_decode ( urldecode ( $_GET ['k'] ), ENT_QUOTES );
			} elseif ($_POST ['k']) {
				$key = $_POST ['k'];
			}
			// 关键字不能超过30个字符
			if (mb_strlen ( $key, 'UTF8' ) > 30)
				$key = mb_substr ( $key, 0, 30, 'UTF8' );
			$_SESSION ['home_user_search_key'] = serialize ( $key );
		
		} else if (is_numeric ( $_GET [C ( 'VAR_PAGE' )] )) {
			$key = unserialize ( $_SESSION ['home_user_search_key'] );
		
		} else {
			//unset($_SESSION['home_user_search_key']);
		}
		//echo unserialize ($_SESSION ['home_user_search_key']);
		return trim ( $key );
	}
}	
?>